import { handlesType, IShapeProps, IType } from "../interface/IShape";

export type Handles = "tl" | "tm" | "tr" | "mr" | "br" | "bm" | "bl" | "ml";

/* 由于可能存在大量创建对象的情况，用class更容易初始化 */
export class BaseShape implements IShapeProps {
  declare x: number;
  declare y: number;
  declare w: number;
  declare h: number;
  declare rotate: number;
  declare originX: number;
  declare originY: number;
  declare minWidth: number;
  declare minHeight: number;
  declare maxWidth: number;
  declare maxHeight: number;
  declare parent: boolean;
  declare zoom: number;
  declare draggable: boolean;
  declare rotatable?: boolean | undefined;
  declare xAxis?: boolean | undefined;
  declare yAxis?: boolean | undefined;
  /* 拖拽步长[x,y,z] */
  declare gap: number[];
  declare resizable: boolean;
  declare z: number;
  declare id: string;
  declare disabled?: boolean | undefined;
  declare preventDeactivation: boolean;
  declare snap: number;
  declare overflow: boolean;
  declare lockAspectRatio: boolean;
  declare scale: number;
  declare flipX?: boolean;
  declare swap?: boolean;
  declare borderColor: string;
  declare className?: string;
  declare type: IType;
  declare axis: "x" | "y" | "both";
  declare grid: [number, number, number];
  declare disableUserSelect: boolean;
  declare active: boolean;
  declare handles: handlesType[];
  declare show: boolean;
  declare lock: boolean;
  declare hasBorder: boolean;
  declare fieldCode: string;

  constructor(config?: Partial<IShapeProps>) {
    const defaultValue: IShapeProps = {
      x: 0,
      y: 0,
      w: 50,
      h: 50,
      rotate: 0,
      originX: 0,
      originY: 0,
      minWidth: -Infinity,
      minHeight: -Infinity,
      maxWidth: Infinity,
      maxHeight: Infinity,
      parent: true,
      grid: [1, 1, 90],
      draggable: true,
      rotatable: true,
      resizable: true,
      disabled: false,
      preventDeactivation: true,
      overflow: false,
      lockAspectRatio: false,
      className: "",
      borderColor: "",
      type: IType.RECT,
      id: nanoid(),
      z: 0,
      axis: "both",
      disableUserSelect: false,
      active: false,
      handles: [],
      show: true,
      lock: false,
      hasBorder: false,
      fieldCode: "",
    };
    Object.assign(this, config, defaultValue);
  }
}
